home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / GDIDIB.PAK / PENDLG.C < prev    next >
C/C++ Source or Header  |  1997-05-06  |  13KB  |  455 lines

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (C) 1993 - 1995  Microsoft Corporation.  All Rights Reserved.
  7. //
  8. //  MODULE:   pendlg.c
  9. //
  10. //  PURPOSE:  Displays the "Pen Style" dialog box
  11. //
  12. //  FUNCTIONS:
  13. //    PenDlg          - Process messages for "Pen Style" dialog box.
  14. //    MsgPenInit      - Initialize the Pen dialog with info from lparam.
  15. //    MsgPenPaint     - Paint the Example Window in the Pen dialog
  16. //    MsgPenCommand   - Process WM_COMMAND messages sent to the Pen dialog.
  17. //    CmdPenStyle     - Track the currently selected pen style.
  18. //    CmdPenColor     - Put up the ChooseColor dialog to select pen color.
  19. //    CmdPenDone      - Free the Pen dialog and related data.
  20. //
  21. //  COMMENTS:
  22. //
  23.  
  24. #include <windows.h>            // required for all Windows applications
  25. #include <windowsx.h>
  26. #include "globals.h"            // prototypes specific to this application
  27. #include "pendlg.h"             // Controls ID's for the Pen dialog
  28. #include "colordlg.h"           // Palette choose color dialog
  29.  
  30. // global variables specific to this module
  31. RECT rcExam;                 // location of example window in dialog
  32.  
  33. // prototypes specific to this module
  34. LRESULT MsgPenInit    (HWND, UINT, WPARAM, LPARAM);
  35. LRESULT MsgPenPaint   (HWND, UINT, WPARAM, LPARAM);
  36. LRESULT MsgPenCommand (HWND, UINT, WPARAM, LPARAM);
  37. LRESULT CmdPenStyle   (HWND, WORD, WORD, HWND);
  38. LRESULT CmdPenColor   (HWND, WORD, WORD, HWND);
  39. LRESULT CmdPenDone    (HWND, WORD, WORD, HWND);
  40.  
  41. // Pen dialog message table definition.
  42. MSD rgmsdPen[] =
  43. {
  44.     {WM_COMMAND,    MsgPenCommand},
  45.     {WM_PAINT,      MsgPenPaint},
  46.     {WM_INITDIALOG, MsgPenInit}
  47. };
  48.  
  49. MSDI msdiPen =
  50. {
  51.     sizeof(rgmsdPen) / sizeof(MSD),
  52.     rgmsdPen,
  53.     edwpNone
  54. };
  55.  
  56. // Pen dialog command table definition.
  57. CMD rgcmdPen[] =
  58. {
  59.     {IDD_PENSTYLE,  CmdPenStyle},   // Pen Style notification msg
  60.     {IDD_PENCOLOR,  CmdPenColor},   // Color button
  61.     {IDOK,          CmdPenDone},    // OK and Cancel buttons
  62.     {IDCANCEL,      CmdPenDone}
  63. };
  64.  
  65. CMDI cmdiPen =
  66. {
  67.     sizeof(rgcmdPen) / sizeof(CMD),
  68.     rgcmdPen,
  69.     edwpNone
  70. };
  71.  
  72.  
  73. //
  74. //  FUNCTION: PenDlg(HWND, UINT, WPARAM, LPARAM)
  75. //
  76. //  PURPOSE:  Processes messages for "Pen Style" dialog box.
  77. //
  78. //  PARAMETERS:
  79. //    hdlg - window handle of the dialog box
  80. //    wMessage - type of message
  81. //    wparam - message-specific information
  82. //    lparam - message-specific information
  83. //
  84. //  RETURN VALUE:
  85. //    TRUE - message handled
  86. //    FALSE - message not handled
  87. //
  88. //  COMMENTS:
  89. //
  90. //
  91.  
  92. LRESULT CALLBACK PenDlg(HWND hdlg, UINT uMessage, WPARAM wparam, LPARAM lparam)
  93. {
  94.     return DispMessage(&msdiPen, hdlg, uMessage, wparam, lparam);
  95. }
  96.  
  97.  
  98. //
  99. //  FUNCTION: MsgPenInit(HWND, UINT, WPARAM, LPARAM)
  100. //
  101. //  PURPOSE: To initialize the Pen dialog with info from lparam.
  102. //
  103. //  PARAMETERS:
  104. //    hwnd - The window handing the message.
  105. //    uMessage - The message number. (unused).
  106. //    wparam - Message specific data (unused).
  107. //    lparam - points to LOGPEN structure.
  108. //
  109. //  RETURN VALUE:
  110. //    Always returns TRUE
  111. //
  112. //  COMMENTS:
  113. //    Sets the initial state of the controls according to the LOGPEN
  114. //      passed in via lparam.
  115. //
  116.  
  117. #pragma argsused
  118. LRESULT MsgPenInit(HWND hdlg, UINT uMessage, WPARAM wparam, LPARAM lparam)
  119. {
  120.     int i, nIndex, nSel;
  121.     HWND hctlStyle;
  122.     char szTmp[32];
  123.     LPLOGPEN lpLP;
  124.  
  125.     // lparam is a pointer to a LOGPEN structure
  126.     lpLP = (LPLOGPEN)lparam;
  127.  
  128.     // Save pointer to LOGPEN structure in window bytes
  129.     SetWindowLong(hdlg, DWL_USER, (LONG)lpLP);
  130.  
  131.     // Center the dialog over the application window
  132.     CenterWindow(hdlg, GetWindow(hdlg, GW_OWNER));
  133.  
  134.     // Fill up the style combobox.  The item data is set to the
  135.     // corresponding pen style defined in windows.h (e.g. PS_SOLID).
  136.  
  137.     hctlStyle = GetDlgItem(hdlg, IDD_PENSTYLE);
  138.     nSel = 0;
  139.  
  140.     for (i = IDD_PENFIRST; i <= IDD_PENLAST; i++)
  141.     {
  142.         LoadString(hInst, i, szTmp, sizeof(szTmp));
  143.         nIndex = SendMessage(hctlStyle, CB_ADDSTRING, 0, (LPARAM)(LPSTR)szTmp);
  144.         SendMessage(hctlStyle, CB_SETITEMDATA, nIndex, i - IDD_PENSTYLE);
  145.  
  146.         // If this item is the current style, remember it
  147.         if (i == IDD_PENSTYLE + (int)lpLP->lopnStyle)
  148.             nSel = nIndex;
  149.     }
  150.  
  151.     // Set the initial style selection
  152.     SendMessage(hctlStyle, CB_SETCURSEL, nSel, 0L);
  153.  
  154.     // Initialize the Width edit control
  155.     SetDlgItemInt(hdlg, IDD_PENWIDTH, lpLP->lopnWidth.x, FALSE);
  156.  
  157.     // Example window always uses 0 width (so styles work)
  158.     lpLP->lopnWidth.x = 0;
  159.  
  160.     // Get coordinates of the example window in the dialog
  161.     GetWindowRect(GetDlgItem(hdlg, IDD_PENEXAMPLE), &rcExam);
  162.     ScreenToClient(hdlg, (LPPOINT)&rcExam);
  163.     ScreenToClient(hdlg, ((LPPOINT)&rcExam) + 1);
  164.  
  165.     // Reduce rect slightly so we don't paint over its frame
  166.     InflateRect(&rcExam, -1, -1);
  167.  
  168.     return TRUE;
  169. }
  170.  
  171.  
  172. //
  173. //  FUNCTION: MsgPenPaint(HWND, UINT, WPARAM, LPARAM)
  174. //
  175. //  PURPOSE: Paint the example window with examples of the current pen
  176. //
  177. //  PARAMETERS:
  178. //    hwnd - The window handling the message.
  179. //    uMessage - The message number. (unused).
  180. //    wparam - Message specific data (unused).
  181. //    lparam - Message specific data (unused).
  182. //
  183. //  RETURN VALUE:
  184. //    Always returns TRUE
  185. //
  186. //  COMMENTS:
  187. //    Sets the initial state of the controls according to the LOGPEN
  188. //    passed in via lparam.
  189. //
  190.  
  191. #pragma argsused
  192. LRESULT MsgPenPaint(HWND hdlg, UINT uMessage, WPARAM wparam, LPARAM lparam)
  193. {
  194.     PAINTSTRUCT ps;
  195.     HPEN hpen, hpenBlack;
  196.     LPLOGPEN lpLP;
  197.     int i, y, dy;
  198.  
  199.     BeginPaint(hdlg, &ps);
  200.  
  201.     // select our logical palette for palette-relative colors to work
  202.     if (hPalette)
  203.         SelectPalette(ps.hdc, hPalette, TRUE);
  204.  
  205.      SetBkMode(ps.hdc, TRANSPARENT);
  206.     SelectObject(ps.hdc, GetStockObject(NULL_BRUSH));
  207.  
  208.     // Get pointer to LOGPEN from window bytes
  209.     lpLP = (LPLOGPEN)GetWindowLong(hdlg, DWL_USER);
  210.  
  211.  
  212.     // Draw some stuff in the example window with pens based on
  213.     // the current LOGPEN (vary the width starting at 0).
  214.  
  215.     #define NUMLINES    5
  216.  
  217.      y = rcExam.top;
  218.     dy = (rcExam.bottom - rcExam.top) / (NUMLINES + 1);
  219.     lpLP->lopnWidth.x = 0;
  220.     hpenBlack = GetStockObject(BLACK_PEN);
  221.  
  222.     for (i = 0; i < NUMLINES; i++)
  223.     {
  224.         lpLP->lopnWidth.x = i*3;
  225.         hpen = CreatePenIndirect(lpLP);
  226.         SelectObject(ps.hdc, hpen);
  227.  
  228.         MoveToEx(ps.hdc, rcExam.left + 10, y += dy, NULL);
  229.           LineTo(ps.hdc, rcExam.right - 10, y);
  230.  
  231.         // Deselect and delete the pen
  232.         SelectObject(ps.hdc, hpenBlack);
  233.         DeleteObject(hpen);
  234.     }
  235.  
  236.     // de-select our logical palette from the DC
  237.     if (hPalette)
  238.         SelectPalette(ps.hdc, GetStockObject(DEFAULT_PALETTE), TRUE);
  239.  
  240.     EndPaint(hdlg, &ps);
  241.      return 0;
  242. }
  243.  
  244.  
  245. //
  246. //  FUNCTION: MsgPenCommand(HWND, UINT, WPARAM, LPARAM)
  247. //
  248. //  PURPOSE: Process WM_COMMAND messages sent to the PenStyle box.
  249. //
  250. //  PARAMETERS:
  251. //    hwnd      - The window handing the message.
  252. //    uMessage  - The message number. (unused).
  253. //    wparam    - Message specific data (unused).
  254. //    lparam    - Message specific data (unused).
  255. //
  256. //  RETURN VALUE:
  257. //    Always returns 0 - message handled.
  258. //
  259. //  COMMENTS:
  260. //    Uses this DipsCommand function defined in wndproc.c combined
  261. //    with the cmdiPen structure defined in this file to handle
  262. //    the command messages for the Pen dialog box.
  263. //
  264.  
  265. #pragma argsused
  266. LRESULT MsgPenCommand(HWND   hwnd,
  267.                              UINT   uMessage,
  268.                              WPARAM wparam,
  269.                              LPARAM lparam)
  270. {
  271.     return DispCommand(&cmdiPen, hwnd, wparam, lparam);
  272. }
  273.  
  274.  
  275. //
  276. //  FUNCTION: CmdPenStyle(HWND, WORD, WORD, HWND)
  277. //
  278. //  PURPOSE: Keeps track of which style button is selected.
  279. //
  280. //  PARAMETERS:
  281. //    hwnd      - The window handling the command.
  282. //    wCommand  - Child control ID (unused).
  283. //    wNotify   - Child notification code.
  284. //    hwndCtrl  - NULL (unused).
  285. //
  286. //  RETURN VALUE:
  287. //    Always returns TRUE.
  288. //
  289. //  COMMENTS:
  290. //
  291.  
  292. #pragma argsused
  293. LRESULT CmdPenStyle(HWND hdlg, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  294. {
  295.     LPLOGPEN lpLP;
  296.     int nSel;
  297.  
  298.     // Get pointer to LOGPEN from window bytes
  299.     lpLP = (LPLOGPEN)GetWindowLong(hdlg, DWL_USER);
  300.  
  301.      // Update the style selection if necessary.
  302.     if (CBN_SELCHANGE == wNotify)
  303.     {
  304.         nSel = SendMessage(hwndCtrl, CB_GETCURSEL, 0, 0L);
  305.         if (CB_ERR != nSel)
  306.         {
  307.             // Save new style
  308.             lpLP->lopnStyle = SendMessage(hwndCtrl, CB_GETITEMDATA, nSel, 0L);
  309.  
  310.             // Repaint the example window
  311.             InvalidateRect(hdlg, &rcExam, TRUE);
  312.         }
  313.      }
  314.  
  315.     return TRUE;
  316. }
  317.  
  318.  
  319. //
  320. //  FUNCTION: CmdPenColor(HWND, WORD, WORD, HWND)
  321. //
  322. //  PURPOSE: Puts up ChooseColor dialog to choose pen color.
  323. //
  324. //  PARAMETERS:
  325. //    hwnd      - The window handling the command.
  326. //    wCommand  - IDD_PENCOLOR (unused).
  327. //    wNotify   - Child notification code (unused).
  328. //    hwndCtrl  - NULL (unused).
  329. //
  330. //  RETURN VALUE:
  331. //    Always returns TRUE.
  332. //
  333. //  COMMENTS:
  334. //
  335.  
  336. #pragma argsused
  337. LRESULT CmdPenColor(HWND hdlg, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  338. {
  339.     LPLOGPEN   lpLP;
  340.     static int nPalIndex=0; // stores system palette index for init'ing
  341.                             // the palette color selection dialog
  342.  
  343.     // Get pointer to LOGPEN from window bytes
  344.     lpLP = (LPLOGPEN)GetWindowLong(hdlg, DWL_USER);
  345.  
  346.     if (bPalDevice)
  347.     {
  348.         // call palette dialog, passing nPalIndex as LPARAM to initialize
  349.           // the correct selection in the dialog
  350.         DialogBoxParam(hInst, "ColorDlg", hdlg, (DLGPROC)Color, (LPARAM)nPalIndex);
  351.  
  352.         if (palinfo.index != -1)
  353.         {
  354.             // save index of color selection
  355.             nPalIndex = palinfo.index;
  356.  
  357.             if (palinfo.index >= 10 && palinfo.index <= 245)
  358.             {
  359.                 // Non-static color was chosen, so use PALETTERGB macro
  360.                 // to save new color
  361.                      lpLP->lopnColor = PALETTERGB(palinfo.red, palinfo.green, palinfo.blue);
  362.             }
  363.             else
  364.             {
  365.                 // Static color was chosen, so just use RGB macro
  366.                 // to save new color
  367.                 lpLP->lopnColor = RGB(palinfo.red, palinfo.green, palinfo.blue);
  368.             }
  369.         
  370.             // Repaint the example window
  371.             InvalidateRect(hdlg, &rcExam, TRUE);
  372.         }
  373.      }
  374.     else // not a palette device, so just use ChooseColor Common Dialog
  375.     {
  376.         CHOOSECOLOR cc;
  377.         static DWORD dwCustColors[16];
  378.  
  379.         // Initialize CHOOSECOLOR struct
  380.         cc.lStructSize      = sizeof(cc);
  381.         cc.hwndOwner        = hdlg;
  382.         cc.hInstance        = NULL;
  383.         cc.rgbResult        = lpLP->lopnColor;
  384.         cc.lpCustColors     = dwCustColors;
  385.           cc.Flags            = CC_RGBINIT;
  386.         cc.lCustData        = 0;
  387.         cc.lpfnHook         = NULL;
  388.         cc.lpTemplateName   = NULL;
  389.  
  390.         if (ChooseColor(&cc))
  391.         {
  392.             // Save new color
  393.             lpLP->lopnColor = cc.rgbResult;
  394.  
  395.             // Repaint the example window
  396.             InvalidateRect(hdlg, &rcExam, TRUE);
  397.           }
  398.     }
  399.  
  400.     return TRUE;
  401. }
  402.  
  403.  
  404. //
  405. //  FUNCTION: CmdPenDone(HWND, WORD, WORD, HWND)
  406. //
  407. //  PURPOSE: Free the PenStyle box and related data.
  408. //
  409. //  PARAMETERS:
  410. //    hwnd      - The window handling the command.
  411. //    wCommand  - The command to be handled.
  412. //    wNotify   - Notification code (unused).
  413. //    hwndCtrl  - NULL (unused).
  414. //
  415. //  RETURN VALUE:
  416. //    Always returns TRUE.
  417. //
  418. //  COMMENTS:
  419. //    Calls EndDialog to finish the dialog session.
  420. //
  421.  
  422. #pragma argsused
  423. LRESULT CmdPenDone(HWND hdlg, WORD wCommand, WORD wNotify, HWND hwndCtrl)
  424. {
  425.      LPLOGPEN lpLP;
  426.     UINT nWidth;
  427.     BOOL bTranslated;
  428.  
  429.     if (IDOK == wCommand)
  430.     {
  431.         // Get pointer to LOGPEN from window bytes
  432.         lpLP = (LPLOGPEN)GetWindowLong(hdlg, DWL_USER);
  433.  
  434.         // Get pen width from edit control
  435.         nWidth = GetDlgItemInt(hdlg, IDD_PENWIDTH, &bTranslated, FALSE);
  436.  
  437.           if (bTranslated)                // Did GetDlgItemInt succeed?
  438.         {
  439.             lpLP->lopnWidth.x = nWidth; // Yes, save new width.
  440.             EndDialog(hdlg, TRUE);      // Exit the dialog (success)
  441.         }
  442.         else                            // Error
  443.         {
  444.             MessageBox(hdlg, "Pen Width must be a non-negative integer.",
  445.                         "Pen Style", MB_ICONINFORMATION | MB_OK);
  446.             SetDlgItemInt(hdlg, IDD_PENWIDTH, lpLP->lopnWidth.x, FALSE);
  447.             SetFocus(GetDlgItem(hdlg, IDD_PENWIDTH));
  448.         }
  449.      }
  450.     else    // Just close the dialog (cancel)
  451.         EndDialog(hdlg, FALSE);
  452.  
  453.     return TRUE;
  454. }
  455.